/**
 * 
 * @Project HRL Simulation
 * @Date 20.06.2011
 * @Author Aebi Pascal, Kocher Marc, Sriranjan George
 * @School BFH-TI Biel, Projekt 1
 *
 */

package elements;

import java.util.ArrayList;
import java.util.List;
import utils.Position;
import utils.Resource;


public class Conveyor {
	
	private Position corner1,corner2,corner3,corner4;
	private int rackNumber,rackLength;
	private List<Resource> resources;
	
	
	/**
	 * this constructor creates a new conveyor with the given parameters 
	 * @param rackNumber number of racks, that the systems has
	 * @param rackLength vertical length of one rack
	 */
	public Conveyor(int rackNumber, int rackLength){
		this.rackNumber = rackNumber;
		this.rackLength = rackLength;
		constructResources();
		calculateCornerPoints();
	}
	
	
	/**
	 * construct all conveyor resources
	 */
	private void constructResources(){
		resources = new ArrayList<Resource>();
		resources.add(new Resource(new Position(1, (4+rackLength)/2-1, 0))); // system entry
		Position pos = new Position(4, 0, 0);
		
		//upper part
		for(int i=0; i<rackNumber; i++){
			if(i%2 != 0){
				this.resources.add(new Resource(new Position(pos.getX(), 0, 0)));
				pos = new Position(pos.getX()+3, 0, 0);
			}
		}
		
		pos = new Position(pos.getX()-5, 0, 0);
		
		//lower part
		for(int i=0; i<rackNumber; i++){
			if(i%2 == 0){
				this.resources.add(new Resource(new Position(pos.getX(), 3+rackLength, 0)));
				pos = new Position(pos.getX()-3, 0, 0);
			}
		}
		
	}
	
	
	/**
	 * calculate corner points
	 */
	private void calculateCornerPoints(){
		int liftGap = (int)(rackNumber/2+0.5); // number of liftspaces in x-axis
		corner1 = new Position(1, 0, 0);
		corner2 = new Position(2+liftGap+rackNumber, 0, 0);
		corner3 = new Position(2+liftGap+rackNumber, rackLength+3, 0);
		corner4 = new Position(1, 3+rackLength, 0);
	}
	
	
	/**
	 * get all resources. Those resources are located one position before the output buffers
	 * and of course on the conveyor.
	 * @return list of resources
	 */
	public List<Resource> getResources(){
		return this.resources;
	}
	
	
	/**
	 * each system gets 4 corners, because the conveyor moves around the racks.
	 * @return list of positions
	 */
	public List<Position> getCornerPositions(){
		List<Position> positions = new ArrayList<Position>();
		positions.add(this.corner1);
		positions.add(this.corner2);
		positions.add(this.corner3);
		positions.add(this.corner4);
		return positions;
	}
	
}
